/*
 * ErrorWindow.java
 *
 * Created on 18. August 2008, 13:20
 */

package de.blitzcoder.collide.error;

import de.blitzcoder.collide.Interface;
import de.blitzcoder.collide.gui.IDEDialog;
import de.blitzcoder.collide.util.HttpPost;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.util.HashMap;
import javax.swing.JOptionPane;

/**
 *
 * @author  blitzcoder
 */
public class ErrorWindow extends IDEDialog {
    
    private ApplicationError error;
    
    /** Creates new form ErrorWindow */
    public ErrorWindow() {
        super(Interface.get(),"Fehler");
        initComponents();
    }

    ErrorWindow(ApplicationError error) {
        this();
        threadLabel.setText(error.getThreadName());
        exceptionLabel.setText(error.getError().getClass().getName());
        timeLabel.setText(error.getDate().toString());
        StringWriter sw = new StringWriter();
        error.getError().printStackTrace(new PrintWriter(sw));
        stackTraceArea.setText(sw.toString());
        setPreferredSize(new Dimension(550,500));
        this.error = error;
        pack();
        setVisible(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        infoPanel = new javax.swing.JPanel();
        jTextArea2 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        timeLabel = new javax.swing.JLabel();
        threadLabel = new javax.swing.JLabel();
        exceptionLabel = new javax.swing.JLabel();
        stackTracePanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        stackTraceArea = new javax.swing.JTextArea();
        actionsPanel = new javax.swing.JPanel();
        sendButton = new javax.swing.JButton();
        copyButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();

        setTitle("CollIDE: Ein Fehler ist aufgetreten");
        setMinimumSize(new java.awt.Dimension(445, 439));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        infoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Info"));
        infoPanel.setLayout(new java.awt.GridBagLayout());

        jTextArea2.setColumns(20);
        jTextArea2.setEditable(false);
        jTextArea2.setLineWrap(true);
        jTextArea2.setRows(5);
        jTextArea2.setText("Ein unerwarteter Fehler ist aufgetreten. Ich bitte sie, mir diesen Fehler zuzusenden, um ein schnelles Beheben zu ermöglichen.");
        jTextArea2.setWrapStyleWord(true);
        jTextArea2.setOpaque(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        infoPanel.add(jTextArea2, gridBagConstraints);

        jLabel1.setText("Zeit: ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        infoPanel.add(jLabel1, gridBagConstraints);

        jLabel2.setText("Thread: ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        infoPanel.add(jLabel2, gridBagConstraints);

        jLabel3.setText("Exception:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        infoPanel.add(jLabel3, gridBagConstraints);

        timeLabel.setText("jLabel4");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 1.0;
        infoPanel.add(timeLabel, gridBagConstraints);

        threadLabel.setText("jLabel5");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 1.0;
        infoPanel.add(threadLabel, gridBagConstraints);

        exceptionLabel.setText("jLabel6");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 1.0;
        infoPanel.add(exceptionLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        getContentPane().add(infoPanel, gridBagConstraints);

        stackTracePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Stack Trace"));
        stackTracePanel.setLayout(new java.awt.GridLayout(1, 0));

        stackTraceArea.setColumns(20);
        stackTraceArea.setEditable(false);
        stackTraceArea.setRows(5);
        jScrollPane1.setViewportView(stackTraceArea);

        stackTracePanel.add(jScrollPane1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(stackTracePanel, gridBagConstraints);

        actionsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Aktionen"));
        actionsPanel.setLayout(new java.awt.GridBagLayout());

        sendButton.setText("Fehler senden");
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        actionsPanel.add(sendButton, gridBagConstraints);

        copyButton.setText("In die Zwischenablage kopieren");
        copyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                copyButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        actionsPanel.add(copyButton, gridBagConstraints);

        closeButton.setText("Schließen");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        actionsPanel.add(closeButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(actionsPanel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
        
        new SubmissionDialog(this, error).setVisible(true);

}//GEN-LAST:event_sendButtonActionPerformed

    private void copyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyButtonActionPerformed
        String s = error.generateError();
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(s), null); 
    }//GEN-LAST:event_copyButtonActionPerformed

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_closeButtonActionPerformed

    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel actionsPanel;
    private javax.swing.JButton closeButton;
    private javax.swing.JButton copyButton;
    private javax.swing.JLabel exceptionLabel;
    private javax.swing.JPanel infoPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JButton sendButton;
    private javax.swing.JTextArea stackTraceArea;
    private javax.swing.JPanel stackTracePanel;
    private javax.swing.JLabel threadLabel;
    private javax.swing.JLabel timeLabel;
    // End of variables declaration//GEN-END:variables
    
}
